<?php

namespace mpend\modules\passport\controllers;

use Yii;
use mpend\components\Controller;
use common\models\user\UserLogout;

/**
 * 账号退出登录
 * 
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class LogoutController extends Controller {

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'status' => ['GET'],
            '*' => ['POST'],
        ];
    }

    /**
     * 退出登录
     * @inheritdoc
     */
    public function actionIndex() {
        if ($this->uid == 0) {
            return true;
        }
        $token = $this->token;
        $token->user_id = 0;
        return $token->save();
    }

    /**
     * 注销申请
     * @inheritdoc
     */
    public function actionApply() {
        $user = Yii::$app->user;
        if (!$user) {
            parent::error('帐号信息不存在');
        }
        $logout = UserLogout::findOrNew([
            'user_id' => $user->id,
        ]);
        $logout->mark = '申请注销帐号';
        $logout->status = UserLogout::STATUS_ACTIVE;
        return $logout->save();
    }

    /**
     * 注销申请
     * @inheritdoc
     */
    public function actionStatus() {
        $user = Yii::$app->user;
        if (!$user) {
            parent::error('帐号信息不存在');
        }
        $apply = UserLogout::find()->active()->andWhere([
            'user_id' => $user->id,
        ])->one();
        $steps = !$apply ? UserLogout::loadSteps() : [];
        return compact('steps', 'apply');
    }

}
